package com.wm.blog_gateway.filter;

import com.wm.blog_common.constatnt.CommonConstant;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/***
 * @ClassName: ServerFilter
 * @Description: 该过滤器在spring security filter  之后,用来给权限数据设定,供下游被转发服务使用
 * @Author: wm_yu
 * @Create_time: 16:26 2020-3-28
 */
@Configuration
@Slf4j
@AllArgsConstructor
public class ServerFilter implements GlobalFilter, Ordered {


    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        String id = exchange.getAttribute("adminId");
        //向headers中放文件，记得build,传给下游被转发服务使用
        ServerHttpRequest request = exchange.getRequest().mutate().header(CommonConstant.AMIND_ID, id).build();
        //将现在的request 变成 exchange对象
        return chain.filter(exchange.mutate().request(request).build());
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
